package com.caogt.microservice.config;

import com.google.common.base.Converter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

/**
 * @Desc TODO
 * @user caogt
 * @Date 2019-06-02-22:19
 */
@Configuration
public class DataParamConverter {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Bean
    public Converter<String, Date> dateConverter(){
        return new Converter<String, Date>() {
            @Override
            protected Date doForward(String s) {
                try {
                    if(s != null){
                        return sdf.parse(s);
                    }
                }catch (ParseException e){
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected String doBackward(Date date) {
                if(date != null){
                    return sdf.format(date);
                }
                return null;
            }
        };
    }
}
